package ils.domain.acquisition.model;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class DeliveryFactoryImpl implements DeliveryFactory
{
    private SessionFactory sessionFactory;
    private static final String GET_DELIVERY_SELLER = "select delivery from Delivery as delivery where delivery.sellerId = ? and delivery.deliveryDate between ? and ? order by delivery.deliveryDate dsc";
    @Override
    public void createDelivery(Delivery delivery)
    {
        if(delivery.getDeliveryDate() == null) delivery.setDeliveryDate(new Date());
        Session session = sessionFactory.getCurrentSession();
        Set<DeliveryItem> deliveryItems = delivery.getItems();
        for(DeliveryItem deliveryItem : deliveryItems)
        {
            OrderItem orderItem = deliveryItem.getOrderItem();
        }
        session.save(delivery);
    }

    @Override
    public List<Delivery> getDeliveriesFor(int sellerId, Date fromDate,
            Date endDate)
    {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(GET_DELIVERY_SELLER);
        query.setInteger(0, sellerId);
        query.setDate(1, fromDate);
        query.setDate(2, endDate);
        return query.list();
    }

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

}
